package com.augurit.wf.jbpmex.task.multitask.completerule;

import java.util.List;

import org.jbpm.api.history.HistoryTask;
import org.jbpm.pvm.internal.history.model.HistoryTaskImpl;
import org.jbpm.pvm.internal.task.TaskImpl;

import com.augurit.common.base.form.ResultForm;
import com.augurit.wf.jbpmex.task.multitask.completerule.base.BaseCompleteRule;

public class ByPercentRule extends BaseCompleteRule {

	private int completePercent;         //要求完成比例
	
	
	public ByPercentRule(int completePercent){
		this.completePercent = completePercent;
	}
	
	public void init(TaskImpl curTask, TaskImpl superTask, List<HistoryTaskImpl> hisChildTasks) {
		super.initParams(curTask, superTask, hisChildTasks);
	}
	
	@Override
	public boolean isEnd() {
		float percent = 0;

		if(hisChildTasks != null && hisChildTasks.size() > 0){
			float count = 0;
			float sum = hisChildTasks.size();
			
			for(HistoryTaskImpl hisChildTask : hisChildTasks){
				if(hisChildTask.getState() != null 
						&& hisChildTask.getState().equals(HistoryTask.STATE_COMPLETED)){
					count++;
				}
			}
			
			percent = count / sum * 100;
		}
		
		if(percent >= completePercent)
			return true;
		
		return false;
	}

	@Override
	public ResultForm validate(String[] userKeys) {
		ResultForm result = new ResultForm(true);
		return result;
	}
}
